<?php
header("content-type:text/html;charset=utf-8");
include "../public/config/config.php";
include "../public/include/funcs.php";
include "../public/include/money.class.php";
$link=conn($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd,$cfg_dbname,$cfg_dbchar);
$obj=new money($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd,$cfg_dbname,$cfg_dbchar);
$id=$_GET['id'];
$self_arr=select($link, "user","id=$id");
$arr_all[]=$self_arr[0];
$tree[]=$self_arr[0];
$self_name=$self_arr[0]['tname'] ? $self_arr[0]['tname'] : $self_arr[0]['nickname'];

function find_id($link,$id){
	global $arr_all;
	$arr=select($link, "user","pid=$id");
	foreach($arr as $v){
		$arr_all[]=$v;
		find_id($link,$v['id']);
	}
}

function get_tree($arr,$pid,$step){
	global $tree;
	foreach($arr as $k=>$v){
		if($v['pid']==$pid){
			$flg=str_repeat('&nbsp; -- &nbsp;', $step);
			$name = $v['tname'] ? $v['tname'] : $v['nickname'];
			$v['name']=$flg.$name;
			$tree[]=$v;
			get_tree($arr, $v['id'], $step+1);
		}
	}
}
find_id($link,$id);
get_tree($arr_all, $id, 1);

foreach($tree as $k=>$v){
	$tree[$k]['self_xf']=$obj->self_xf($v['id']);
	$tree[$k]['all_xf']=$obj->all_xf($v['id']);
}

/*$self_xf=$obj->self_xf($id);
var_dump($self_xf);
$all_xf=$obj->all_xf($id);
var_dump($all_xf);*/

echo "<pre>";
print_r($self_arr);
//print_r($all_xf);
print_r($tree);
echo "</pre>";
?>
<html>
	<head>
		<title><?php echo $self_name;?>的子级结构</title>
		<link rel="stylesheet" type="text/css" href="../public/css/main.css"/>
	</head>
	<body>
<div class="jiegou">
	<h2><?php echo $self_name;?>的子级结构</h2>
	<h3><?php echo $self_name." 总消费额为 ".$tree[0]['self_xf']." 元，总业绩为 ".$tree[0]['all_xf']." 元";?></h3>
	<a target="_blank" href="see_d3_tree.php">查看完整数状图</a><hr />
<?php
foreach($tree as $v){
	$url_id=$v['id'];
	$name = $v['name'];
	echo "<p><a target='_blank' href='index.php?pid=$url_id'>".$name."</a> --> 自己消费：<span>[".$v['self_xf']."]</span> --> 总业绩：<span>{".$v['all_xf']."}</span>";
//	echo  "</p>";
	echo " <span id='liter'>( 自己的ID：".$v['id']."- 上级ID：</span><span id='liter'>".$v['pid'].")</span></p>";
}
?>
</div>

	</body>
</html>